<?php
namespace Avalanche\Action;

use Avalanche\Action;
use Avalanche\Model\RequestModel;
use Avalanche\Model\ResponseModel;

/**
 * Index action show statistic information about request system
 *
 * @author Vitold Sedyshev
 */
class Index extends Action {

    /**
     * Handle connection
     *
     * @param \Avalanche\Model\RequestModel $request
     * @return \Avalanche\Model\ResponseModel
     */
    public function execute($request) {
        static $connectionNumber = 0;

        $response = new \Avalanche\Model\ResponseModel();
        $response->setHeader('Content-type', 'application/json');
        $response->setHeader('Connection', 'close');
        $response->setHeader('Pragma', 'no-cache');
        $response->setHeader('Server', 'Avalanche/0.1 (' . PHP_OS . ') mod_php/' . phpversion());

        // Generate response
        $result = array(
            'connectionNumber' => $connectionNumber,
            'currentTime' => microtime(true),
            'uri' => $request->getURI(),
            'method' => $request->getMethod(),
        );
        $content = json_encode($result);
        $response->setBody($content);

        $connectionNumber++;

        return $response;
    }

}